﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;

namespace ProductSpecViewer
{
    class PhoneViewer
    {
        /// <summary>
        /// Extension file to lookup when reading. It is a static value.
        /// </summary>
        const string LOOKUP_EXTENSION = ".xml";
        ObservableCollection<Phone> collection;
        /// <summary>
        /// A collection data of phones.
        /// </summary>
        public ObservableCollection<Phone> Collection
        {
            get { return this.collection; }
        }
        public PhoneViewer()
        {
            Initialize();
        }
        protected void Initialize()
        {
            this.collection = new ObservableCollection<Phone>();

            //retrieve from file structure.
            string location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            Retrieve(location);

            //bind all phone data into collection.
        }
        private void Retrieve(string folder)
        {
            DirectoryInfo info = new DirectoryInfo(folder);
            foreach (FileInfo file in info.GetFiles())
            {
                if (file.Extension.ToLower().CompareTo(LOOKUP_EXTENSION) == 0)
                    Binding(file.FullName);
            }
            foreach (DirectoryInfo d in info.GetDirectories())
                Retrieve(d.FullName);
        }
        private void Binding(string file)
        {
            Phone phone = Phone.LoadFromFile(file);
            phone.Load();
            this.collection.Add(phone);
        }
    }
}